\section{The Social Planner Problem in Discrete Time}
\label{app:B}
Denote the corresponding co-state
variables for difference equations (\ref{eq:kinc}) - (\ref{eq:Hinc}) by $q$, $\sigma$, $\nu$ and $\eta$. Let $\lambda$ be the
Lagrange multiplier on the resource constraint and $\epsilon$ on the energy constraint. We also need to allow
for the possibility that either type of energy source is not used and
investment in cost reduction for the energy technology is zero. To
that end, let $\mu$ the multiplier on the constraint $j\geq 0$,
$\omega$ the multiplier on the constraint $n\geq 0$, $\xi$ the
multiplier on the constraint $R\geq 0$ and $\zeta$ the multiplier on
the constraint $B\geq 0$.

Assuming technology progress in renewable sector ends at $T_2$,
the Lagrangian is defined by

\begin{align}
  \label{eq:ch2Lagrangian}
  \mathcal{L} = \sum_{t=0}^{T_2-1} \beta^t \biggl\lbrace
  \frac{c_t^{1-\gamma}}{1-\gamma}+
  \lambda_t[Ak_t-c_t-i_t-j_t-n_t-g(S_t,N_t)R_t-(\Gamma_1+H_t)^{-\alpha}B_t]
  \nonumber \\
  +\epsilon_t(R_t+B_t-Ak_t)+
  \mu_tj_t+\omega_t n_t+\xi_tR_t+\zeta_tB_t+q_t[i_t+(1-\delta)k_t-k_{t+1}]
  \nonumber \\
  +\eta_t(H_t+B_t^{\psi}j_t^{1-\psi}-H_{t+1})+
  \sigma_t(S_t+Q_tR_t-S_{t+1})+\nu_t(N_t+n_t-N_{t+1})\biggr\rbrace
  \nonumber\\
  +\sum_{t=T_2}^{\infty}\beta^t \biggl\lbrace
  \frac{c_t^{1-\gamma}}{1-\gamma}+
  lambda_t[Ak_t-c_t-i_t-\Gamma_2B_t]+\epsilon_t(B_t-Ak_t)+q_t[i_t+(1-\delta)
  \nonumber\\
  k_t-k_{t+1}]+\eta_t(H_t-H_{t+1})+
  \sigma_t(S_t-S_{t+1})+\nu_t(N_t-N_{t+1}) \biggr\rbrace.
\end{align}

When $T\leq T_2$, The first order conditions for a maximum with
respect to the control variables are:
\begin{equation}
\frac{\partial \mathcal{L}}{\partial c}=  c_t^{-\gamma}-\lambda_t=0
\label{eq:FOC2c}
\end{equation}
\begin{equation}
\frac{\partial \mathcal{L}}{\partial i}= q_t-\lambda_t = 0
\label{eq:FOC2i}
\end{equation}
\begin{equation}
\frac{\partial \mathcal{L}}{\partial j}=  (1-\psi)\eta_tB_t^{\psi}j_t^{-\psi}-\lambda_t + \mu_t = 0; \mu_tj_t=0, \mu_t\ge 0, j_t \ge 0
\label{eq:FOC2j}
\end{equation}
\begin{equation}
\label{eq:FOC2n}
\frac{\partial \mathcal{L}}{\partial n}= -\lambda_t +\nu_t+\omega_t = 0, \omega_t
n_t=0, \omega_t\ge 0, n_t\ge 0
\end{equation}
\begin{equation}
\label{eq:FOC2R}
\frac{\partial \mathcal{L}}{\partial R}= -\lambda_t g(S_t,N_t)+\epsilon_t+\sigma_t
Q_t+\xi_t = 0, \xi_t R_t=0, \xi_t\ge 0, R_t\ge 0
\end{equation}
\begin{equation}
\label{eq:FOC2B}
\frac{\partial \mathcal{L}}{\partial B}=   \epsilon_t+\psi\eta B_t^{\psi-1}j_t^{1-\psi}-\lambda_t(\Gamma_1+H_t)^{-\alpha}+\zeta_t=0, \zeta_t
B_t=0, \zeta_t\ge 0, B_t\ge 0.
\end{equation}

The first order conditions for a maximum with respect to the next
period state variables are:
\begin{align}
\label{eq:FOC2k}
  \frac{\partial{\mathcal{L}}}{\partial k_{t+1}} &=
A\lambda_{t+1}-A\epsilon_{t+1}+(1-\delta)q_{t+1}-\frac{q_t}{\beta} = 0 \\
  \label{eq:FOC2H}
  \frac{\partial{\mathcal{L}}}{\partial H_{t+1}} &=
\lambda_{t+1}\alpha(\Gamma_1+H_{t+1})^{-\alpha-1}B_{t+1}+\eta_{t+1}-\frac{\eta_t}{\beta} = 0 \\
  \label{eq:FOC2S}
  \frac{\partial{\mathcal{L}}}{\partial S_{t+1}} &=
-\lambda_{t+1}R_{t+1}\frac{\partial g_{t+1}}{\partial S_{t+1}}+\sigma_{t+1}-\frac{\sigma_t}{\beta} = 0 \\
 \label{eq:FOC2N}
  \frac{\partial{\mathcal{L}}}{\partial N_{t+1}} &=
-\lambda_{t+1}R_{t+1}\frac{\partial g_{t+1}}{\partial N_{t+1}}+\nu_{t+1}-\frac{\nu_t}{\beta} = 0.
\end{align}

We begin our detailed analysis with the last regime, describing economic growth once the technological limit in energy production is reached.

\subsubsection{The long run endogenous growth economy}
In the last regime, the model becomes a simple endogenous growth model
with investment only in physical capital.  For all $t \geq T_2$, The
second part of the Lagrangian formula applies. The first-order
conditions \eqref{eq:FOC2c}, \eqref{eq:FOC2i} and \eqref{eq:FOC2k}
still hold, except the one with respect to $B$ simplifies to:
\begin{align}
  \label{eq:FOC2B1}
  \frac{\partial{\mathcal{L}}}{\partial B_t} &=
  \epsilon_t-\Gamma_2\lambda_t = 0.
\end{align}

Given $\epsilon_t = \Gamma_2\lambda_t$ from \eqref{eq:FOC2B1}, we could get the Euler equation combining with \eqref{eq:FOC2c} and \eqref{eq:FOC2k}, 
\begin{align}
  \label{eq:eq1_analytical}
    \frac{c_t^{-\gamma}}{\beta c_{t+1}^{-\gamma}} =
(1-\Gamma_2)A+1-\delta = \bar{A}.
\end{align}
 And the resource constraint changes to
\begin{align}
  \label{eq:RC1}
  c_t+i_t+\Gamma_2B_t = Ak_t.
\end{align}

By solving the equations \eqref{eq:eq1_analytical}, \eqref{eq:RC1}
and the difference equation with $k$, one will have
\begin{align}
  k_{t+1}+c_t = \bar{A}k_t.
\end{align}
Then the problem can be solved analytically $\forall t \geq T_2$, to get the limiting policy function: \begin{align}
  k_{t+1} &= (\beta\bar{A})^{1/\gamma}k_t. 
\end{align}

According to the definition, $H$ reaches its upper limit at $T_2$, and for all $t\geq T_2$, we have 
\begin{align}
  \label{eq:upperlimit_H}
  H_t = \Gamma _{2}^{-1/\alpha}-\Gamma _{1}.
\end{align}

%   \label{eq:terminalV} V(k_t) &=
%   \frac{[1-\bar{A}(\beta\bar{A})^{-1/\gamma}]^{-\gamma}}{\beta(1-\gamma)}k_t^{1-\gamma}
% \end{align}

% The terminal value function \eqref{eq:terminalV} will be used for
% calculating $V$ at $T_2$ given $k_{T_2}$. Also, it might
% give us a hint of the form of value functions in previous regimes.


\subsubsection{Renewable regime with technology progress}

Working backwards in time, we consider next the regime where $B=Ak>0, j>0$ and $H<\Gamma _{2}^{-1/\alpha}- \Gamma _{1}$. In this regime, the economy just transits from fossil fuel energy to
renewable energy, and the energy cost declines by both learning by
doing and direct investment in technology progress. There are two state variables in this regime: the physical capital $k$ and the cumulative knowledge in renewable energy sector $H$. First-order conditions \eqref{eq:FOC2c} to \eqref{eq:FOC2j}, \eqref{eq:FOC2B} to \eqref{eq:FOC2H} apply.

Observing from \eqref{eq:FOC2j}, so long as $B>0$ we must also have $j^\psi\lambda\ge(1-\psi)\eta B^ \psi>0$. Also, when $j>0$, it must satisfy
\begin{equation}
\label{eq:jsoln}
j_t=[(1-\psi_t)(\eta_t/\lambda_t)]^{1/\psi}B_t,  
\end{equation}
Hence, $j$ also becomes positive for the first time at $T_1$ and we must also have $H=0$ at $T_1$.  

Substituting the expression of $\epsilon$ into \eqref{eq:FOC2k} and  combining the result with equations \eqref{eq:FOC2c} and \eqref{eq:FOC2i}, we obtain the
first Euler equation\footnote{Where $j_{t+1} =
(\hat{H}-H_{t+1})^{\frac{1}{1-\psi}}(Ak_{t+1})^{-\frac{\psi}{1-\psi}}$
for all $t < T_2-1$. Note that eliminating $j_{t+1}$ brings the
``two-period-ahead'' value of $H$, denoted as $\hat{H}$. Hence, we will have $j_{T_2} = 0$.},
\begin{align}
  \label{eq:EulerkRenew}
  \frac{c_t^{-\gamma}}{\beta c_{t+1}^{-\gamma}} &=
  A-(\Gamma_1+H_{t+1})^{-\alpha}A + 1-\delta+
  \frac{\psi j_{t+1}}{(1-\psi)k_{t+1}}.
\end{align}
The second Euler equation could be obtained from \eqref{eq:FOC2c}, \eqref{eq:FOC2i}, \eqref{eq:FOC2j} and \eqref{eq:FOC2H}:
\begin{align}
  \label{eq:EulerHRenew}
  \frac{c_t^{-\gamma}}{\beta c_{t+1}^{-\gamma}} &=
A^{\psi}k_t^{\psi}j_t^{-\psi} \left[
    (1-\psi)\alpha Ak_{t+1}(\Gamma_1+H_{t+1})^{-\alpha-1} +
    A^{-\psi}k_{t+1}^{-\psi}j_{t+1}^{\psi}
  \right].
\end{align}

Once the Euler equations are solved with resource constraint
\eqref{eq:Budget}, state equations \eqref{eq:kinc} and \eqref{eq:Hinc}, and appropriate boundary conditions, we
can attain the optimum policy paths.


\subsubsection{Fossil Fuel Economy Regime}
Finally, we consider the initial regime where $R>0$. Then \eqref{eq:FOC2R} implies $\xi =0$ and the shadow price of energy will be
\begin{equation}
\epsilon =\lambda g(S,N)-\sigma Q.  \label{eq:epsilon_fossil}
\end{equation}

The co-state variable $\sigma=0$ at $T_1$ since $S$ has no effect once fossil fuels cease to be used. Also, because an increase in $S$ raises the cost of fossil fuel while fossil fuels are used, $\sigma$ is negative for $t<T_1$. Hence, \eqref{eq:FOC2R} implies that the shadow price of energy exceeds  $\lambda g(S,N)$ for $t<T_1$ but converges to it as $t\rightarrow T_1$. Thus, at $T_1$, \eqref{eq:epsilon_fossil}, \eqref{eq:FOC2B} and continuity of the shadow price of energy at $T_1$ require
\begin{equation}
\epsilon_t = \lambda_t g_t(S,N)=\lambda_t\Gamma_1^{-\alpha}-\psi\eta_t B_t^{\psi-1}j_t^{1-\psi}.
\label{eq:conteps}
\end{equation}
In particular, \eqref{eq:conteps} implies that the transition from fossil fuels to renewable energy will occur when the mining cost of fossil fuel energy, $ g(S,N)$, is strictly less than the initial cost of renewable energy $\Gamma_1^{-\alpha}$. Thus, the benefits of learning by doing make it worthwhile to transit to renewable energy before the cost of fossil fuels reaches parity with the cost of renewable energy.

While we have proved we cannot have zero direct R\&D investment $j$ in renewable sector while the production $B$ is positive in previous section, we do have a regime where investment in fossil fuel technology $n=0$ while fossil fuels continue to be used ($R>0$). Specifically, since changes in $N$, like changes in $S$, have no effect once the economy abandons fossil fuels at $T_1$, the co-state variable $\nu$ corresponding to $N$ will be zero. On the other hand, \eqref{eq:FOC2i} implies $\lambda=q>0$, so from \eqref{eq:FOC2n}, $\omega=\lambda-\nu>0$. For $t<T_1$, increases in $N$ will reduce fossil fuel mining costs and raise the maximized value of the objective subject to the constraints. So that $\nu>0$ along the time. As we move backwards in time from $T_1$, $\nu$ will be increasing while $\lambda$ is decreasing. Hence, we will arrive at a time $T_0<T_1$ when $\nu=\lambda$, and for $t<T_0$ we will have $n>0$ in addition to $R>0$. From \eqref{eq:FOC2n}, we will also continue to have $\nu=\lambda$ for $t<T_0$.

For the period $[T_0, T_1]$ with $n=0$, $N_t = N_{t+1}$. The first-order conditions \eqref{eq:FOC2c}, \eqref{eq:FOC2i}, \eqref{eq:FOC2R}, \eqref{eq:FOC2k}, and \eqref{eq:FOC2S} apply. 

Substituting \eqref{eq:epsilon_fossil} into \eqref{eq:FOC2k}, with \eqref{eq:FOC2c} and \eqref{eq:FOC2i}, we have an Euler equation in fossil fuel regime:
\begin{align}
\frac{c_t^{-\gamma}}{\beta c_{t+1}^{-\gamma}} =  (1-g_{t+1}+\frac{\sigma_{t+1}Q_{t+1}}{\lambda_{t+1}})A+1-\delta.
\label{eq:Euler_k_Fossil}
\end{align}
And the resource constraint is simplified as
\begin{align}
  \label{eq:Budget2}
  c_t + i_t + g(S_t,N_t)R_t  = y_t.
\end{align}

Given state equations \eqref{eq:kinc}, \eqref{eq:Sinc}, \eqref{eq:FOC2S}, \eqref{eq:Euler_k_Fossil} and \eqref{eq:Budget2}, the dynamic system could be solved in this period.

For the period $[0, T_0]$ with $n>0$, besides the Euler equation \eqref{eq:Euler_k_Fossil}, equation \eqref{eq:FOC2n} now can be used as well. From equation \eqref{eq:FOC2c}, \eqref{eq:FOC2n} and \eqref{eq:FOC2N}, we have another Euler equation:
\begin{align}
  \label{eq:Euler_N_Fossil}
  \frac{c_t^{-\gamma}}{\beta c_{t+1}^{-\gamma}} &= 1-\frac{\partial g_{t+1}}{\partial N_{t+1}}Ak_{t+1}
\end{align}

With equations \eqref{eq:Ninc} and \eqref{eq:Euler_N_Fossil} that are only true in this period, plus all state equations used in previous period, we could solve this equation system and obtain the optimal policy paths. 

% \section{The Numerical Algorithm and Methodology}
% \label{sec:numer-solut-proc}%
%  Using MATLAB, I start at $T_2$ and solve the difference equations backward. The numerical procedure consists of the following steps:

% \begin{enumerate}

% % \item Discretize the data of continuous time model into yearly
% %   data. First I generate cubic smoothing splines $f_{fossil}$ and
% %   $f_{renew}$ to the given data in continuous time model for both
% %   energy regimes, then I use command {\it fnval} to obtain the
% %   corresponding function value at the points in $t=1,\dotsm,T_2$.

% \item \label{item:iterstart} Make the initial guesses of $T_2$ and
%   $k_{T_2}$. Solve equation \eqref{eq:eq1_analytical}-\eqref{eq:upperlimit_H} to obtain $c_{T_2}$, $i_{T_2}$ and $H_{T_2}$.

% \item \label{item:iterR} In a reverse chronological order, starting at
%   $t = T_2-1$, given $k_{t+1}$, $H_{t+1}$ and $j_{t+1}$,
%   apply Euler equations \eqref{eq:EulerkRenew} and
%   \eqref{eq:EulerHRenew} and the resource constraint
% to get $k$, $c$ and $j$ at $t$. Then calculate
% $H_t$ by \eqref{eq:Hdiff}.

% \item Repeat step \ref{item:iterR} until $H_t$ hits zero. Denote $t =
%   T_0$, which is the transition point from the use of fossil fuel to
%   renewable energy. Then I move on to the fossil fuel regime
%   backward.

% \item Make the initial guess of $S_{T_0}$. At $T_0$, solve
%   equation \eqref{eq:condition_T0} to get $N_{T_0}$, given the value of
%   other variables following from renewable regime.

% \item \label{item:iterF} Starting at $t = T_0-1$, given all the
%   variables in $t+1$ period, I solve equations
%   \eqref{eq:Sdiff},~\eqref{eq:FOCS}, \eqref{eq:Euler_N_Fossil},
%   \eqref{eq:Euler_k_Fossil} and ~\eqref{eq:RC_Fossil} to get $c$, $k$,
%   $S$, $N$, and $\sigma$. Then calculate all the other desired
%   variables $i$, $n$, etc.

% \item Repeat step \ref{item:iterF} until $t=0$.
% \item Return to step \ref{item:iterstart} and iterate until $k_0$,
%   $S_0$ and $N_0$ at time $0$ hit the target value, which is
%   calibrated from data.
% \end{enumerate}

